package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.entity.elastic.DataImportRecord;
import cn.fintecher.pangolin.entity.elastic.ImportTemplate;
import cn.fintecher.pangolin.entity.elastic.ImportTemplateItem;
import cn.fintecher.pangolin.entity.elastic.PreRecordModel;
import cn.fintecher.pangolin.service.dataimp.model.ImportCommonModel;
import cn.fintecher.pangolin.service.dataimp.model.ImportModel;
import cn.fintecher.pangolin.service.dataimp.service.BaseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 14:53 2019/11/5
 */

@Service("importPreFuture")
public class ImportPreFuture {
    @Autowired
    private BaseService baseService;

    /**
     * 多线程处理委前催记excel数据
     */
    @Async
    public CompletableFuture<List<ImportModel>> processPreRecordExcel(List<Map<String, String>> mapList, int sheetIndex, String batchNumber, Map<String,
            ImportTemplateItem> itemMap, Integer num, DataImportRecord record, ImportTemplate importTemplate) {
        int row = importTemplate.getDataStartRow() + num;
        List<ImportModel> models = new ArrayList<>();
        for (Map<String, String> map : mapList) {
            ImportModel model = new ImportModel();
            PreRecordModel preRecordModel = new PreRecordModel();
            ImportCommonModel importCommonModel = new ImportCommonModel();
            importCommonModel.setPreRecordModel(preRecordModel);
            List<String> errors = new ArrayList<>();
            for (Map.Entry<String, ImportTemplateItem> entry : itemMap.entrySet()) {
                baseService.rowToObj(importCommonModel, entry, map, row, errors, sheetIndex);
            }
            //判断必填项
            Date date = preRecordModel.getDate();
            if (Objects.isNull(date)) {
                errors.add("第【" + row + "】行中的数据【催记日期】对应值为空");
            }
            String content = preRecordModel.getContent();
            if (StringUtils.isBlank(content)) {
                errors.add("第【" + row + "】行中的数据【催记内容】对应值为空");
            }
            String certificateNo = preRecordModel.getCertificateNo();
            if (StringUtils.isBlank(certificateNo)) {
                errors.add("第【" + row + "】行中的数据【证件号】对应值为空");
            }
            preRecordModel.setId(UUID.randomUUID().toString().replaceAll("-", "")); //主键ID
            preRecordModel.setBatchNumber(batchNumber); //批次号
            preRecordModel.setPrincipalId(record.getPrincipalId()); //委托方ID
            preRecordModel.setPrincipalName(record.getPrincipalName()); //委托方名称
            model.setPreRecordModel(preRecordModel);
            model.setErrors(errors);
            models.add(model);
            row++;
        }
        return CompletableFuture.completedFuture(models);
    }
}
